package com.ysm.smfu.chat;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysm.smfu.MainActivity;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.BaseActivity;
import com.ysm.smfu.bean.Friend;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.manager.GlideCircleTransform;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.hyphenate.chat.EMClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chatuidemo.db.DBManager;
import chatuidemo.db.InviteMessgeDao;

/**
 * Created by Administrator on 2016/10/25.
 */
public class DetailFriendActivity extends BaseActivity {
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.search_img)
    ImageView searchImg;
    @BindView(R.id.search_nc)
    TextView searchNc;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.search_sr)
    TextView searchSr;
    @BindView(R.id.search_dq)
    TextView searchDq;
    @BindView(R.id.delFriend)
    TextView delFriend;
    private Friend friend;
    private Intent intent;
    private String name;
    private String hxId;

    @Override
    protected void init() {
        super.init();
        friend = (Friend) getIntent().getSerializableExtra("detailFriend");
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        hxId = currShared.getString(SpKey.HXID, "");
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.detailfriend2);
        ButterKnife.bind(this);
        titleBody.setText("用户详情");
        mainActivityTopbarImg.setVisibility(View.GONE);
        if (TextUtils.equals(friend.hxId, hxId)) {
            delFriend.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        if (friend != null) {
            if (!TextUtils.isEmpty(friend.imageAddress) && !friend.imageAddress.startsWith("null?"))
                Glide.with(DetailFriendActivity.this).load(friend.imageAddress).placeholder(R.drawable.myimg2).error(R.drawable.myimg2)
                        .transform(new GlideCircleTransform(UIUtils.getContext())).into(searchImg);
            if (TextUtils.isEmpty(friend.petName))
                searchNc.setText("未填写");
            else
                searchNc.setText(friend.petName);
            if (TextUtils.isEmpty(friend.sex))
                sex.setText("未填写");
            else
                sex.setText(friend.sex);

            if (TextUtils.isEmpty(friend.birthday))
                searchSr.setText("未填写");
            else
                searchSr.setText(friend.birthday);
            if (TextUtils.isEmpty(friend.address))
                searchDq.setText("未填写");
            else
                searchDq.setText(friend.address);
        } else {
            searchNc.setText("未填写");
            sex.setText("未填写");
            searchSr.setText("未填写");
            searchDq.setText("未填写");
        }
    }

    @OnClick({R.id.emptyHistory, R.id.delFriend})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.emptyHistory:
                emptyHistory();
                break;
            case R.id.delFriend:
                delFriend();
                break;
        }
    }

    /**
     * 删除好友
     * /user/deleteFirend
     * id: 用户id   firendId :好友id
     */
    private void delFriend() {
        BaseProtocal.builder(true).delFriend(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("删除好友", response);
                if (!TextUtils.isEmpty(response)) {
                    String state = JsonUtil.getState(response);
                    if (TextUtils.equals(state, "1")) {
                        //                从数据库中去除好友
                        DBManager.getInstance().deleteContact2(friend.id);
                        DBManager.getInstance().deleteShenqingContact(friend.id);
                        //删除和某个user会话，如果需要保留聊天记录，传false
                        EMClient.getInstance().chatManager().deleteConversation(friend.hxId, true);
                        InviteMessgeDao inviteMessgeDao = new InviteMessgeDao(UIUtils.getContext());
                        inviteMessgeDao.deleteMessage(friend.hxId);
                        UIUtils.startActivity(MainActivity.class);
                    }
                }
            }

            @Override
            public void onFail(Exception e) {

            }
        }, friend.id);
    }

    protected void emptyHistory() {
        String msg = getResources().getString(R.string.Whether_to_empty_all_chats);
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage(msg)
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EMClient.getInstance().chatManager().deleteConversation(friend.hxId, true);
                        // 清空聊天记录，发送广播刷新页面
                        sendBroad();
                        finish();
                    }
                }).create();
        dialog.show();
    }

    /**
     * 发送广播，刷新聊天记录
     */
    private void sendBroad() {
        if (intent == null) {
            intent = new Intent();
            intent.setAction("refreshHistory");
        }
        sendBroadcast(intent);
    }

}
